﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SLGameEngine
{
    public abstract partial class ObjectBase : UserControl
    {
        public string Description = Consts.Unknown;
        public string ObjectName = Consts.Unknown;
        public bool IsWalkable = true;
        public int BaseObjectID = Consts.None;
        public bool IsTargetable = false;
        public bool IsAttackable = false;

        public bool AnimateInEditor = false;

        private double _currentX = 0.0;
        private double _currentY = 0.0;

        public double DestX { get; set; }
        public double DestY { get; set; }

        public double CurrentX
        {
            set
            {
                _currentX = value;
                this.SetValue(Canvas.LeftProperty, _currentX);
            }
            get { return _currentX; }
        }

        public double CurrentY
        {
            set
            {
                _currentY = value;
                this.SetValue(Canvas.TopProperty, _currentY);
            }
            get { return _currentY; }
        }
        
        public ObjectBase(double x, double y, int baseObjID)
        {
            InitializeComponent();

            BaseObjectID = baseObjID;

            this.CurrentX = x;
            this.CurrentY = y;
        }

        public bool IsAtDest()
        {
            return (CurrentX == DestX && CurrentY == DestY);
        }
      
        public void SetWidth(double width)
        {
            this.Width = width;
            SelectionRect.Width = width;
            CollisionRect.Width = width;
            ObjClip.Rect = new Rect(0, 0, this.Width, this.Height);
        }

        public void SetHeight(double height)
        {
            this.Height = height;
            SelectionRect.Height = height;
            CollisionRect.Height = height;
            ObjClip.Rect = new Rect(0, 0, this.Width, this.Height);
        }

        public bool DetectCollision(Point pt, ref Point selectionPoint)
        {
            double x = CurrentX;
            double y = CurrentY;

            x += (double)CollisionRect.GetValue(Canvas.TopProperty);
            y += (double)CollisionRect.GetValue(Canvas.LeftProperty);

            bool collide = pt.X >= x && pt.Y >= y && pt.X < (x + CollisionRect.ActualWidth) && pt.Y < (y + CollisionRect.ActualHeight);

            if (true == collide)
            {
                selectionPoint.X = pt.X - x;
                selectionPoint.Y = pt.Y - y;
            }
            return collide;
        }

        // if your derived class support Preview rendering override this function
        public virtual void ShowPreview() { }

        protected virtual void CopyToClone(int maxRadius, ref ObjectBase clone)
        {
            clone.Width = Width;
            clone.Height = Height;
            clone.Description = Description;
            clone.ObjectName = ObjectName;
            clone.IsWalkable = IsWalkable;
            clone.IsTargetable = IsTargetable;
            clone.IsAttackable = IsAttackable;
        }

        public abstract ObjectBase Clone(int maxRadius);
        public abstract void Deselect();
        public abstract ObjectBase Select();
    }
}
